/* File:        	ReqResChannel.java
* Package:			org.esi.authz.comm
* Created:      	29-abr-2011
* Last Changed: 	0:08:09
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.comm.channel;

import java.io.*;
import java.net.*;
import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.ResponseContext;

import com.tecnalia.authz.common.comm.IProtocol;
import com.tecnalia.authz.common.comm.ProtocolImpl;
import com.tecnalia.authz.common.log.PDPLog;
import com.tecnalia.authz.pdp.core.PDPEngine;
import com.tecnalia.authz.pdp.core.PDPInstances;
import com.tecnalia.authz.pdp.core.xacml.XACMLQueryImpl;
import com.tecnalia.comm.channel.IDChannel;

// TODO: Auto-generated Javadoc
/**
 * The Class ReqResChannel.
 */
public class ReqResDChannelImpl extends Thread implements IDChannel{
	   
   	/** The client. */
   	private Socket client = null;
	   
   	/** The ois. */
   	private ObjectInputStream ois = null;
	   
   	/** The oos. */
   	private ObjectOutputStream oos = null;
	   
   	/** The logger. */
   	public PDPLog logger = new PDPLog(this.getClass());	
	
   	/** The data. */
	   private IDChannelData data = null;

   	/** The protocol. */
	   private IProtocol protocol = null;

   	/* (non-Javadoc)
	 * @see org.esi.authz.comm.IDaemonChannel#run()
	*/
	@Override
	public void Connect(Socket clientSocket) {
	
		client = clientSocket;
	    try {
	    	ois = new ObjectInputStream(client.getInputStream());
	    	oos = new ObjectOutputStream(client.getOutputStream());
	    } catch(Exception e1) {
	    	try {
	    		client.close();
	        }catch(Exception e) {
	        	this.logger.info(e.getMessage());
	        }
	        return;
	     }
	    
	     this.start();
	}

   	/**
	    * Sets the protocol.
	    *
	    * @param proto the new protocol
	    */
	   public void setProtocol(IProtocol proto){
   		this.protocol = proto;
   	}

	
	/* (non-Javadoc)
	 * @see org.esi.authz.comm.IDaemonChannel#run()
	 */
	@Override
	public void run() {
		   IProtocol protocol = null;
		   try {
	        //Read from InputBuffer
		   protocol = (IProtocol) ois.readObject();

		   data = new DChannelDataImpl();
		   data.doProcess(protocol);

			
			oos.writeObject(protocol);
	        oos.flush();
	        
	        // close connections
	        /*ois.close();
	        oos.close();
	        client.close();*/
	        
	        
	      } catch(Exception e) {
	    	  this.logger.info(e.getMessage());
	      }       
	   }
	   
	   
	   
	   
	/* (non-Javadoc)
	 * @see org.esi.authz.comm.IDaemonChannel#cleanup()
	 */
	@Override
	public void cleanup() {

        // close connections
        try {
			ois.close();
	        oos.close();
	        client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	   
	   /*public ResponseContext Evaluate(RequestContext request) throws Exception {
	   
	        IDaemon sample = null;
	        ResponseContext res = null;
	        //Retrieve the configuration 
	        PDPConfiguration authzCfg = new PDPConfiguration();
	        try {
	        	//Create a PDP according to the configuration
				sample = new DaemonImpl(authzCfg);
				
				//Start the daemon
				sample.start();

				String[] recurso = {"recurso1"};

				//Create the list to hold the request
				res = sample.evaluateRequest(request);


	        }
	        catch (Throwable t) {
	            t.printStackTrace(System.err);
	            
	            if (sample != null) {
	                sample.cleanup();
	            }
	        }
	        
			return res;	        

	    	
	    }	*/		

	   
	   
	}
